{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "orig_nbformat": 4,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.9.4 64-bit"
  },
  "interpreter": {
   "hash": "7ad59aab27c906dbbf0c2e1dfcaac35fb186b016969018ec3a977f4c21569b21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "source": [
    "1.随机数生成六个班的考试成绩，3门考试：Python、数学、语文。每个班50人"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [],
   "source": [
    "#6个班级，50名学生，3门成绩 \n",
    "class_all = np.random.randint(40,151,size=(6,50,3)) "
   ]
  },
  {
   "source": [
    "2.将六个班的考试成绩进行合并得到score"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "300"
      ]
     },
     "metadata": {},
     "execution_count": 122
    }
   ],
   "source": [
    "#将六个班的考试成绩进行合并得到score\n",
    "score = np.concatenate([i for i in class_all],axis=0)\n",
    "len(score)  "
   ]
  },
  {
   "source": [
    "3.生成性别数组sex，水平叠加数组sex和score得到data"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    "#生成性别组  男生为0，女生为1\n",
    "sex = np.random.randint(0,2,size=(300,1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [],
   "source": [
    "#水平叠加数组sex和score得到data\n",
    "data = np.hstack((score,sex))\n",
    "# date[:30]"
   ]
  },
  {
   "source": [
    "4.分别计算男女生各科成绩统计指标：最小值、最大值、平均分、中位数、标准"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "# date[:,0] Python\n",
    "# date[:,1] 数学\n",
    "# date[:,2] 语文"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义各科成绩所需值得函数 方便后面调用\n",
    "def jieguo(num):\n",
    "    # num[:,0] Python\n",
    "    p_min = num[:,0].min() #最小值\n",
    "    p_max = num[:,0].max()  #最大值\n",
    "    p_mean = num[:,0].mean()   #平均分\n",
    "    p_median = np.median(num[:,0])  #中位数\n",
    "    p_std = np.std(num[:,0])    #标准差\n",
    "    print('python课程：最小值：%d，最大值：%d，平均分：%.2f，中位数：%.2f，标准差：%.2f' % (p_min,p_max,p_mean,p_median,p_std))\n",
    "    # num[:,1] 数学\n",
    "    m_min = num[:,1].min() #最小值\n",
    "    m_max = num[:,1].max()  #最大值\n",
    "    m_mean = num[:,1].mean()   #平均分\n",
    "    m_median = np.median(num[:,1])  #中位数\n",
    "    m_std = np.std(num[:,1])    #标准差\n",
    "    print('数学课程：最小值：%d，最大值：%d，平均分：%.2f，中位数：%.2f，标准差：%.2f' % (m_min,m_max,m_mean,m_median,m_std))\n",
    "    # num[:,2] 语文\n",
    "    c_min = num[:,2].min() #最小值\n",
    "    c_max = num[:,2].max()  #最大值\n",
    "    c_mean = num[:,2].mean()   #平均分\n",
    "    c_median = np.median(num[:,2])  #中位数\n",
    "    c_std = np.std(num[:,2])    #标准差\n",
    "    print('语文课程：最小值：%d，最大值：%d，平均分：%.2f，中位数：%.2f，标准差：%.2f' % (c_min,c_max,c_mean,c_median,c_std))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "男生人数为： 142 人\npython课程：最小值：40，最大值：150，平均分：97.07，中位数：96.00，标准差：32.08\n数学课程：最小值：40，最大值：149，平均分：96.24，中位数：97.50，标准差：31.30\n语文课程：最小值：42，最大值：150，平均分：99.45，中位数：98.00，标准差：31.12\n"
     ]
    }
   ],
   "source": [
    "#男生\n",
    "male = data[np.where(data[:,3]==0,)]\n",
    "print('男生人数为：',len(male),'人')\n",
    "jieguo(male)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "女生人数为： 158 人\npython课程：最小值：40，最大值：150，平均分：92.20，中位数：89.50，标准差：32.66\n数学课程：最小值：40，最大值：150，平均分：93.15，中位数：93.00，标准差：32.89\n语文课程：最小值：40，最大值：146，平均分：93.18，中位数：91.00，标准差：29.92\n"
     ]
    }
   ],
   "source": [
    "#女生\n",
    "female = data[np.where(data[:,3]==1,)]\n",
    "print('女生人数为：',len(female),'人')\n",
    "jieguo(female)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}